<template>
  <basicTopContainer>
    <el-tabs v-model="activeName" slot="tabs" tab-position="bottom">
      <el-tab-pane label="仪表计费" name="first"></el-tab-pane>
    </el-tabs>
    <el-button slot="button" type="primary" size="small" @click="showTemplate('add')">新增模板</el-button>
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :page="page"
        :permission="permissionList"
        :before-open="beforeOpen"
        v-model="form"
        ref="crud"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @on-load="onLoad"
      >
        <template slot="menu" slot-scope="{size, type, row}">
          <el-button
            :type="type"
            :size="size"
            v-if="permission.calculationtemplate_delete"
            @click="showTemplate('view', row)"
          ><i class="el-icon-view"></i> 查看</el-button>
          <el-button
            :type="type"
            :size="size"
            v-if="permission.calculationtemplate_delete"
            @click="showTemplate('edit', row)"
          ><i class="el-icon-edit"></i> 编辑</el-button>
          <el-button
            :type="type"
            :size="size"
            v-if="permission.calculationtemplate_delete"
            @click="rowDel(row)"
          ><i class="el-icon-delete"></i> 删除</el-button>
        </template>
      </avue-crud>
    </basic-container>
    <add-template :ids="ids" :type="type" :onLoad="onLoad" v-model="templateVisable"></add-template>
  </basicTopContainer>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove,
} from "@/api/meter/calculationtemplate";
import { mapGetters } from "vuex";
import addTemplate from "./components/calculationtemplate/addTemplate";

export default {
  data() {
    return {
      ids: 0,
      activeName: "first",
      type: "add", // add: 新增 edit：编辑 view：查看
      templateVisable: false,
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        tip: false,
        header: false,
        searchShow: false,
        searchMenuSpan: 6,
        index: true,
        viewBtn: true,
        selection: true,
        dialogClickModal: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        addBtn: false,
        column: [
          {
            label: "模板名称",
            prop: "templateName",
            rules: [
              {
                required: true,
                message: "请输入模板名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "计费模式",
            prop: "feeType",
            type: "select",
            dicData: [
              {
                label: "无",
                value: 0,
              },
              {
                label: "平均",
                value: 1,
              },
              {
                label: "峰谷",
                value: 2,
              },
              {
                label: "阶梯",
                value: 3,
              },
            ],
            rules: [
              {
                required: true,
                message: "请输入计费方式：1、平均；2、峰谷；3、阶梯",
                trigger: "blur",
              },
            ],
          },
          // {
          //   label: "计费类型",
          //   prop: "feeType",
          //   value: '电费'
          // },
          {
            label: "结算周期",
            prop: "settlementCycle",
            formatter: (row, value)=> {
              return `${value}天`
            },
            rules: [
              {
                required: true,
                message: "请输入结算周期",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.calculationtemplate_add, false),
        viewBtn: this.vaildData(
          this.permission.calculationtemplate_view,
          false
        ),
        delBtn: this.vaildData(
          this.permission.calculationtemplate_delete,
          false
        ),
        editBtn: this.vaildData(
          this.permission.calculationtemplate_edit,
          false
        ),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  methods: {
    // 2020/8/20 新增
    showTemplate(type, row) {
      if (type != "add") {
        this.ids = row.id;
      }
      this.type = type;
      this.templateVisable = true;
    },
    rowSave(row, done, loading) {
      console.log(row)
      let _data = {
        feeType: row.feeType, // 计费模式
        ladderedPriceList: {
          // price: 
        }
      }
      return
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      page = page || this.page;
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
  },
  components: {
    addTemplate,
  },
};
</script>

<style>
</style>
